package xmu.crms.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import xmu.crms.entity.*;
import xmu.crms.exception.*;
import xmu.crms.service.ClassService;
import xmu.crms.service.CourseService;
import xmu.crms.service.FixGroupService;
import xmu.crms.service.UserService;
import xmu.crms.vo.*;


import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * @author badcode
 * @date 2017/11/29
 */

@RestController
@RequestMapping("/class")
public class ClassController {

    @Autowired
    private ClassService classService;

    @Autowired
    private FixGroupService fixGroupService;

    @Autowired
    private UserService userService;

    @Autowired
    private CourseService courseService;

    @GetMapping
    @ResponseStatus(HttpStatus.OK)
    public List<CourseClassVO> getClassList(String courseName, String courseTeacher, String all,
                                            @RequestAttribute("userId") String userId)
            throws ClassesNotFoundException, UserNotFoundException, CourseNotFoundException {
        List<CourseClassVO> courseClasses = new ArrayList<>();
        if(courseName != null || courseTeacher != null) {
            List<ClassInfo> classInfos = classService.listClassByName(courseName, courseTeacher);
            for (ClassInfo classInfo : classInfos) {
                CourseClassVO courseClassVO = new CourseClassVO(classInfo);
                courseClasses.add(courseClassVO);
            }
        } else if (all != null) {
            User student = userService.getUserByUserId(new BigInteger(userId));
            List<ClassInfo> classInfos=classService.listClassBySchool(student.getSchool().getId());
            for (ClassInfo classInfo : classInfos) {
                System.out.println(classInfo);
                CourseClassVO courseClassVO=new CourseClassVO(classInfo);
                courseClasses.add(courseClassVO);
            }
        } else {
            List<ClassInfo> classInfos = classService.listClassByUserId(new BigInteger(userId));
            for (ClassInfo classInfo :classInfos) {
                CourseClassVO courseClassVO=new CourseClassVO(classInfo);
                courseClasses.add(courseClassVO);
            }
        }
        return courseClasses;
    }

    @GetMapping("/{classId}")
    @ResponseStatus(HttpStatus.OK)
    public ClassInfoVO getClass(@PathVariable("classId") String id)
            throws ClassesNotFoundException, CourseNotFoundException {
        ClassInfo classInfo = classService.getClassByClassId(new BigInteger(id));
        ClassInfoVO classInfoVO = new ClassInfoVO(classInfo);
        if (null == classInfo.getFivePointPercentage()) {
            Course course = courseService.getCourseByCourseId(classInfo.getCourse().getId());
            Proportions proportions = new Proportions();
            proportions.setC(course.getThreePointPercentage());
            proportions.setB(course.getFourPointPercentage());
            proportions.setA(course.getFivePointPercentage());
            proportions.setReport(course.getReportPercentage());
            proportions.setPresentation(course.getPresentationPercentage());
            classInfoVO.setProportions(proportions);
        }
        return classInfoVO;
    }

    @PutMapping("/{classId}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void updateClass(@PathVariable("classId") String classId,
                            @RequestBody ClassInfoVO newClass) throws ClassesNotFoundException {
        ClassInfo classInfo = newClass.transferToClassInfo();
        classService.updateClassByClassId(new BigInteger(classId), classInfo);
    }

    @DeleteMapping("/{classId}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void deleteClass(@PathVariable("classId") String classId) throws ClassesNotFoundException {
        classService.deleteClassByClassId(new BigInteger(classId));
    }

    @GetMapping("/{classId}/student")
    @ResponseStatus(HttpStatus.OK)
    public List<StudentVO> getStudentList(@PathVariable String classId) {
        return null;
    }

    @PostMapping("/{classId}/student")
    @ResponseStatus(HttpStatus.CREATED)
    public UrlVO chooseClass(@RequestBody User student,
                             @PathVariable("classId") String classId) throws UserNotFoundException, ClassesNotFoundException {
        BigInteger id = classService.insertCourseSelectionById(student.getId(), new BigInteger(classId));
        UrlVO urlVO = new UrlVO();
        urlVO.setUrl("/class/" + classId + "/student/" + id.toString());
        return urlVO;
    }

    @DeleteMapping("/{classId}/student/{studentId}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void cancelClass(@PathVariable("classId") String classId,
                            @PathVariable("studentId") String studentId) throws UserNotFoundException, ClassesNotFoundException {
        classService.deleteCourseSelectionById(new BigInteger(studentId), new BigInteger(classId));
    }

    @GetMapping("/{classId}/classgroup")
    @ResponseStatus(HttpStatus.OK)
    public GroupMemberVO getClassGroup(@PathVariable("classId") String classId,
                                       @RequestAttribute("userId") String userId) throws UserNotFoundException, ClassesNotFoundException, FixGroupNotFoundException{
        FixGroup fixGroup = fixGroupService.getFixedGroupById(new BigInteger(userId), new BigInteger(classId));
        GroupMemberVO groupMemberVO = new GroupMemberVO();
        groupMemberVO.setLeader(new StudentVO(fixGroup.getLeader()));
        List<StudentVO> members = new ArrayList<>();
        List<FixGroupMember> fixGroupMembers = fixGroupService.listFixGroupByGroupId(fixGroup.getId());
        for (FixGroupMember fixGroupMember : fixGroupMembers) {
            if (fixGroupMember.getId().compareTo(fixGroup.getLeader().getId()) != 0) {
                members.add(new StudentVO(fixGroupMember.getStudent()));
            }
        }
        groupMemberVO.setMembers(members);
        return groupMemberVO;
    }

    @PutMapping("/{classId}/classgroup/resign")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void resignLeader(@PathVariable("classId") int classId,
                             @RequestBody User student) {
    }

    @PutMapping("/{classId}/classgroup/assign")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void assignLeader(@PathVariable("classId") int classId,
                             @RequestBody User student) {
    }

    @PutMapping("/{classId}/classgroup/add")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void addMember(@PathVariable("classId") String classId,
                          @RequestBody User student, @RequestAttribute("userId") String userId) throws UserNotFoundException,
            InvalidOperationException, FixGroupNotFoundException, ClassesNotFoundException {
        FixGroup fixGroup = fixGroupService.getFixedGroupById(new BigInteger(userId), new BigInteger(classId));
        if (fixGroup.getLeader().getId().toString().equals(userId)) {
            throw new InvalidOperationException("权限不足");
        }
        fixGroupService.insertStudentIntoGroup(student.getId(), fixGroup.getId());
    }

    @PutMapping("/{classId}/classgroup/remove")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void removeMember(@PathVariable("classId") String classId,
                             @RequestBody User student) throws UserNotFoundException, ClassesNotFoundException, FixGroupNotFoundException {
        FixGroup fixGroup = fixGroupService.getFixedGroupById(student.getId(), new BigInteger(classId));
        fixGroupService.deleteFixGroupUserById(fixGroup.getId(), student.getId());
    }
}
